package org.cbg.panel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.cbg.bean.DeleteMsgBean;
import org.cbg.btn.TrslationBtn;
import org.cbg.entity.Message;
import org.cbg.frame.TrslationMainJframe;
import org.cbg.until.TraslationTableMyMessageUntil;
import org.come.Frame.ZhuFrame;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;

import com.tool.tcpimg.UIUtils;

 

/**
 * 藏宝阁消息面板
 * 
 * @author Administrator
 * 
 */
public class TraslationMyMessageJpanel extends JPanel {

    /**
     * 页码，全选框，勾选
     */
    private JLabel yema, quanxuankuang, gouxuan;

    /**
     * 页码按钮，首页，末页 , 左箭头, 右箭头,删除消息
     */
    private TrslationBtn shouye, moye, leftArrows, rightArrows, deleteMessage;

    private JScrollPane jScrollPane;
    /**
     * 当前页码 最大页码 , 勾选状态(0勾选，1未勾选)
     *
     */
    private int dangqianyeshu = 1, zuidayema = 50, gouxuanzhuangtai = 1;

    public TraslationMyMessageJpanel() {
        this.setPreferredSize(new Dimension(760, 460));
        this.setOpaque(false);
        this.setLayout(null);



        jScrollPane = new JScrollPane();
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(8, 62, 750, 375+26);
        jScrollPane.setBorder(null);
        TraslationTableMyMessageUntil.TableModel(jScrollPane, 10);
        this.add(jScrollPane);

        /**
         * 勾选状态
         */
        gouxuan = new JLabel();
        gouxuan.setBounds(32, 37, 18, 18);
        gouxuan.setOpaque(false);
        gouxuan.setIcon(null);
        gouxuan.setName("1");
        this.add(gouxuan);

        /**
         * 勾选全选框
         */
        gouxuan.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if ("1".equals(gouxuan.getName())) {
                    gouxuan.setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                    gouxuan.setName("0");

                    TrslationMainJframe.getTrslationMainJframe().setXiaoxiGouxuangeshu(
                            TrslationMainJframe.getTrslationMainJframe().getXiaoxiGeshuMax());
                    /**
                     * 全选时所有的消息勾选框都要勾选
                     */
                    JPanel view = (JPanel) jScrollPane.getViewport().getView();
                    Component[] components = view.getComponents();
                    for (int i = 0; i < components.length; i++) {
                        JPanel panel = (JPanel) components[i];
                        JLabel component2 = (JLabel) panel.getComponent(3);
                        component2.setName("0");
                        component2.setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                    }
                } else {
                    gouxuan.setIcon(null);
                    gouxuan.setName("1");

                    TrslationMainJframe.getTrslationMainJframe().setXiaoxiGouxuangeshu(0);
                    /**
                     * 全选时所有的消息勾选框都要取消勾选
                     */
                    JPanel view = (JPanel) jScrollPane.getViewport().getView();
                    Component[] components = view.getComponents();
                    for (int i = 0; i < components.length; i++) {
                        JPanel panel = (JPanel) components[i];
                        JLabel component2 = (JLabel) panel.getComponent(3);
                        component2.setName("1");
                        component2.setIcon(null);
                    }
                }
            }
        });

        /**
         * 全选框 quanxuankuang
         */
//        quanxuankuang = new JLabel();
//        quanxuankuang.setBounds(25, 12, 15, 15);
//        quanxuankuang.setOpaque(false);
//        quanxuankuang.setIcon(CutButtonImage.getImage(imageMap.getImgMap().get("gxk"),"gxk", 17, 16));
//        this.add(quanxuankuang);

        /**
         * 页码设置
         */
        yema = new JLabel(dangqianyeshu + "/" + zuidayema);
        yema.setBounds(258+91, 442+27, 58, 17);
        yema.setForeground(UIUtils.COLOR_cbg8);
        yema.setOpaque(false);

        yema.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(yema);

        shouye = new TrslationBtn("inkImg/number/b4/fy1.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "");
        shouye.setBounds(287, 442+27, 40, 20);
        this.add(shouye);
        shouye.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

                /**
                 * 跳转到首页
                 */
                if (dangqianyeshu != 1) {
                    dangqianyeshu = 1;
                    yema.setText(dangqianyeshu + "/" + zuidayema);
                    /**
                     * 消息初始化 上传角色Id 上传页码 转换为对应协议头的json对象 发送信息
                     */
                    Message message = new Message();
                    message.setPage(dangqianyeshu);
                    String sendmes = Agreement.getAgreement().searchNewsAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(message));
                    SendMessageUntil.toServer(sendmes);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已在首页");
                }

            }
        });

        leftArrows = new TrslationBtn("inkImg/number/b4/fy2.png", 1);
        leftArrows.setBounds(326, 442+27, 19, 20);
        this.add(leftArrows);
        leftArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu > 1) {
                    dangqianyeshu--;
                    yema.setText(dangqianyeshu + "/" + zuidayema);
                    /**
                     * 消息初始化 上传角色Id 上传页码 转换为对应协议头的json对象 发送信息
                     */
                    Message message = new Message();
                    message.setPage(dangqianyeshu);
                    String sendmes = Agreement.getAgreement().searchNewsAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(message));
                    SendMessageUntil.toServer(sendmes);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有上一页了");
                }
            }
        });

        moye = new TrslationBtn("inkImg/number/b4/fy4.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "");
        moye.setBounds(433, 442+27, 40, 20);
        this.add(moye);

        moye.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu != zuidayema) {
                    dangqianyeshu = zuidayema;
                    yema.setText(dangqianyeshu + "/" + zuidayema);
                    /**
                     * 消息初始化 上传角色Id 上传页码 转换为对应协议头的json对象 发送信息
                     */
                    Message message = new Message();
                    message.setPage(dangqianyeshu);
                    String sendmes = Agreement.getAgreement().searchNewsAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(message));
                    SendMessageUntil.toServer(sendmes);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已在末页");
                }
            }
        });

        rightArrows = new TrslationBtn("inkImg/number/b4/fy3.png", 1);
        rightArrows.setBounds(411, 442+27, 19, 20);
        this.add(rightArrows);
        rightArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu < zuidayema) {
                    dangqianyeshu++;
                    yema.setText(dangqianyeshu + "/" + zuidayema);
                    /**
                     * 消息初始化 上传角色Id 上传页码 转换为对应协议头的json对象 发送信息
                     */
                    Message message = new Message();
                    message.setPage(dangqianyeshu);
                    String sendmes = Agreement.getAgreement().searchNewsAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(message));
                    SendMessageUntil.toServer(sendmes);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有下一页了");
                }
            }
        });
        deleteMessage = new TrslationBtn("inkImg/number/b4/scxxaj.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "");
        deleteMessage.setBounds(12, 1, 95, 26);
        this.add(deleteMessage);

        deleteMessage.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

                /**
                 * 初始化发送对象List<消息表ID> 获取勾选个数以及对应的消息表Id
                 */
                DeleteMsgBean deleteMsgBean = new DeleteMsgBean();
                List<BigDecimal> mesIdList = new ArrayList<>();
                JPanel view = (JPanel) jScrollPane.getViewport().getView();
                Component[] components = view.getComponents();
                for (int i = 0; i < components.length; i++) {
                    JPanel panel = (JPanel) components[i];
                    JLabel component2 = (JLabel) panel.getComponent(3);
                    if (component2.getName().equals("0")) {
                        JLabel component3 = (JLabel) panel.getComponent(0);
                        BigDecimal mesId = new BigDecimal(component3.getName());
                        mesIdList.add(mesId);
                    }
                }
                if (mesIdList.size() == 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择要删除的消息");
                    return;
                }
                deleteMsgBean.setIds(mesIdList);

                TrslationMainJframe.getTrslationMainJframe().setXiaoxiGeshuMax(
                        TrslationMainJframe.getTrslationMainJframe().getXiaoxiGeshuMax()
                                - TrslationMainJframe.getTrslationMainJframe().getXiaoxiGouxuangeshu());
                TrslationMainJframe.getTrslationMainJframe().setXiaoxiGouxuangeshu(0);
                /**
                 * 转换为对应协议的json字符串 提交给服务器处理
                 */
                String sendmes = Agreement.getAgreement().searchDeleteNewsAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(deleteMsgBean));
                SendMessageUntil.toServer(sendmes);
            }
        });
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        if (icon == null) {
            icon = new ImageIcon("inkImg/number/b4/xxxq.png");

        }
        g.drawImage(icon.getImage(), 0, 5, 760, 55, this);


    }

    /**
     * 刷新页面的页码
     *
     * @param dangqianyema
     * @param zuidayema
     *
     */
    public void setPage(int zuidayema) {
        this.zuidayema = zuidayema;
        yema.setText(this.dangqianyeshu + "/" + this.zuidayema);
    }

    public JLabel getYema() {
        return yema;
    }

    public void setYema(JLabel yema) {
        this.yema = yema;
    }

    public JLabel getQuanxuankuang() {
        return quanxuankuang;
    }

    public void setQuanxuankuang(JLabel quanxuankuang) {
        this.quanxuankuang = quanxuankuang;
    }

    public JLabel getGouxuan() {
        return gouxuan;
    }

    public void setGouxuan(JLabel gouxuan) {
        this.gouxuan = gouxuan;
    }

    public TrslationBtn getShouye() {
        return shouye;
    }

    public void setShouye(TrslationBtn shouye) {
        this.shouye = shouye;
    }

    public TrslationBtn getMoye() {
        return moye;
    }

    public void setMoye(TrslationBtn moye) {
        this.moye = moye;
    }

    public TrslationBtn getLeftArrows() {
        return leftArrows;
    }

    public void setLeftArrows(TrslationBtn leftArrows) {
        this.leftArrows = leftArrows;
    }

    public TrslationBtn getRightArrows() {
        return rightArrows;
    }

    public void setRightArrows(TrslationBtn rightArrows) {
        this.rightArrows = rightArrows;
    }

    public TrslationBtn getDeleteMessage() {
        return deleteMessage;
    }

    public void setDeleteMessage(TrslationBtn deleteMessage) {
        this.deleteMessage = deleteMessage;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public JScrollPane getjScrollPane() {
        return jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }

    public int getDangqianyeshu() {
        return dangqianyeshu;
    }

    public void setDangqianyeshu(int dangqianyeshu) {
        this.dangqianyeshu = dangqianyeshu;
    }

    public int getZuidayema() {
        return zuidayema;
    }

    public void setZuidayema(int zuidayema) {
        this.zuidayema = zuidayema;
    }

    public int getGouxuanzhuangtai() {
        return gouxuanzhuangtai;
    }

    public void setGouxuanzhuangtai(int gouxuanzhuangtai) {
        this.gouxuanzhuangtai = gouxuanzhuangtai;
    }

}
